package com.baomidou.springboot.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springboot.entity.Contact;
import com.baomidou.springboot.mapper.ContactMapper;
import com.baomidou.springboot.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 联系我们 服务实现类
 * </p>
 *
 * @author QQQ
 * @since 2018-06-19
 */
@Service
public class ContactServiceImpl implements ContactService {

    @Autowired
    private ContactMapper contactMapper;

    @Override
    public void save(Contact contact) {
        contact.setCreateDate(new Date());
        contactMapper.insert(contact);
    }

    @Override
    public void delete(String id) {
        contactMapper.deleteById(id);
    }

    @Override
    public Contact edit(String id) {
        return contactMapper.selectById(id);
    }

    @Override
    public void update(Contact contact) {
        contact.setModifyDate(new Date());
        contactMapper.updateById(contact);
    }

    @Override
    public Contact view(String id) {
        return contactMapper.selectById(id);
    }

    @Override
    public Page<Contact> list(Page<Contact> page, String search) {
        if (0 == page.getSize()) {
            page.setSize(10);
        }
        if (0 == page.getCurrent()) {
            page.setCurrent(1);
        }
        List<Contact> contactList = contactMapper.findByDateDesc(page, search);
        page.setRecords(contactList);
        return page;
    }

    @Override
    public void deleteBatch(List<String> ids) {
        if (null == ids) {
            return;
        }
        contactMapper.deleteBatchIds(ids);
    }

    @Override
    public Contact detail() {
        return contactMapper.detail();
    }
}
